package pkg.genericity.test;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import pkg.genericity.DO.Criteria;
import pkg.genericity.DO.House;

public class MemoryTestBench {
	public long calculateMemoryUsage(Class<?> clazz) {
		try{
			@SuppressWarnings("unused")
			Object handle = clazz.newInstance();
			long mem0 = Runtime.getRuntime().totalMemory() -
					Runtime.getRuntime().freeMemory();
			long mem1 = Runtime.getRuntime().totalMemory() -
					Runtime.getRuntime().freeMemory();
			handle = null;
			System.gc(); System.gc(); System.gc(); System.gc();
			System.gc(); System.gc(); System.gc(); System.gc();
			System.gc(); System.gc(); System.gc(); System.gc();
			System.gc(); System.gc(); System.gc(); System.gc();
			mem0 = Runtime.getRuntime().totalMemory() -
					Runtime.getRuntime().freeMemory();
			handle = clazz.newInstance();
			System.gc(); System.gc(); System.gc(); System.gc();
			System.gc(); System.gc(); System.gc(); System.gc();
			System.gc(); System.gc(); System.gc(); System.gc();
			System.gc(); System.gc(); System.gc(); System.gc();
			mem1 = Runtime.getRuntime().totalMemory() -
					Runtime.getRuntime().freeMemory();
			return mem1 - mem0;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	  
	  @Test
	  public void testMemoryUsage()
	  {
		  assertTrue(calculateMemoryUsage(House.class) < 150);
		  assertTrue(calculateMemoryUsage(Criteria.class) < 30);
	  }
	  
}